package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.GmBillInfo;
import com.ruoyi.system.domain.GmTenantInfo;
import com.ruoyi.system.mapper.GmBillInfoMapper;
import com.ruoyi.system.service.IGmBillInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

@Service
public class GmBillInfoServiceImpl implements IGmBillInfoService {

    @Autowired
    GmBillInfoMapper gmBillInfoMapper;

    @Override
    public List<GmBillInfo> selectGmBillInfoList(GmBillInfo billInfo) {
        return gmBillInfoMapper.selectGmBillInfoList(billInfo);
    }

    @Override
    public int insertGmBillInfo(GmBillInfo billInfo) {
        billInfo.setBillId(UUID.randomUUID().toString().replaceAll("-", ""));
        concatBillTime(billInfo);
        return gmBillInfoMapper.insertGmBillInfo(billInfo);
    }

    @Override
    public int updateGmBillInfo(GmBillInfo billInfo) {
        concatBillTime(billInfo);
        return gmBillInfoMapper.updateGmBillInfo(billInfo);
    }

    @Override
    public GmBillInfo selectGmBillInfoById(String billId) {
        return gmBillInfoMapper.selectGmBillInfoById(billId);
    }

    @Override
    public int deleteGmBillInfoByIds(String ids) {
        return gmBillInfoMapper.deleteGmBillInfoByIds(Convert.toStrArray(ids));
    }

    @Override
    public String validateDatas(List<GmBillInfo> lists) {
        StringBuilder sb =new StringBuilder();
        if (StringUtils.isEmpty(lists)){
            sb.append("excel中无任何数据，请确认文件！");
        }else {
            for (int i = 0; i <lists.size() ; i++) {
                if (StringUtils.isBlank(lists.get(i).getCompanyName())){
                    sb.append("第"+(i+1)+"行、用户单位名称为空！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getProjectName())){
                    sb.append("第"+(i+1)+"行、用户项目名称为空！<br/>");
                }
//                if (StringUtils.isBlank(lists.get(i).getChargeType())){
//                    sb.append("第"+(i+1)+"行、收费类型为空！<br/>");
//                }
//                if (Double.isNaN(lists.get(i).getBillCount())){
//                    sb.append("第"+(i+1)+"行、开票金额为空！<br/>");
//                }
//                if (Double.isNaN(lists.get(i).getReceivedCount())){
//                    lists.get(i).setReceivedCount(lists.get(i).getBillCount());
//                }
//                if (StringUtils.isBlank(lists.get(i).getBillNumber())){
//                    sb.append("第"+(i+1)+"行、发票号为空！<br/>");
//                }
                if (StringUtils.isBlank(lists.get(i).getReceivedStatus())){
                    lists.get(i).setReceivedStatus("0");
                }
            }
        }
        if (sb.length()>0){
            return sb.toString();
        };
        return "";
    }

    @Override
    public String importRelations(List<GmBillInfo> lists, boolean updateSupport, SysUser sysUser) {
        int successNum = 0;
        int failureNum = 0;
        StringBuffer successMsg = new StringBuffer();
        StringBuffer failureMsg = new StringBuffer();
        for (GmBillInfo sb:lists){
            concatBillTime(sb);
            try {
                if (StringUtils.isNotEmpty(sb.getBillId())){
                    System.out.println(gmBillInfoMapper.selectGmBillInfoById(sb.getBillId()));
                    if (gmBillInfoMapper.selectGmBillInfoById(sb.getBillId()) == null) {
                        gmBillInfoMapper.insertGmBillInfo(sb);
                    }else {
                        gmBillInfoMapper.updateGmBillInfo(sb);
                    }
                }else {
                    sb.setBillId(UUID.randomUUID().toString().replaceAll("-", ""));
                    gmBillInfoMapper.insertGmBillInfo(sb);
                }
                successNum++;
                successMsg.append("<br/>第" + (successNum+failureNum) + "条、" + sb.getBillId()+ "导入成功");
            }catch (Exception e){
                failureNum ++;
                failureMsg.append("<br/>第"+(successNum+failureNum)+"条、导入失败");
            }
        }
        if(failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共"+failureNum+"条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您数据已全部导入成功！共"+ successNum +"条，数据如下：");
        }
        return successMsg.toString();
    }

    /* 拼接开票时间开始+结束 */
    public void concatBillTime(GmBillInfo billInfo){
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy.MM.dd");
        Date start = billInfo.getBillingTimeStart();
        Date end = billInfo.getBillingTimeEnd();
        if(start !=null && end != null ){
            billInfo.setBillingTimeConcat(sbf.format(start)+"-"+sbf.format(end));
        }else if (start !=null && end == null){
            billInfo.setBillingTimeConcat(sbf.format(start)+"-"+"  -");
        }else if (start ==null && end != null){
            billInfo.setBillingTimeConcat("-  "+"-"+sbf.format(end));
        }else {
            billInfo.setBillingTimeConcat("-  "+"-"+"  -");
        }
    }

}
